package com.paic.egis.smts.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;   
import java.util.Calendar;   
import java.util.Date;   


/**  
 *         <p>  
 *         类说明:提取身份证相关信息  
 *         </p>  
 */  
public class IdcardInfoExtractor extends IdcardValidator{   
    // 省份   
    private String province;   
    // 城市   
    private String city;   
    // 区县   
    private String region;   
    // 年份   
    private int year;   
    // 月份   
    private int month;   
    // 日期   
    private int day;   
    // 性别   
    private String gender;   
    // 出生日期   
    private String birthday;   
     

    /**  
     * 通过构造方法初始化各个成员属性  
     * @throws Exception 
     */  

    @SuppressWarnings("static-access")
	public IdcardInfoExtractor(String idcard) { 
    	String idcard18 = idcard;
        if (isValidatedIdcard(idcard)) {   
                if (idcard.length() == 15) {   
                	idcard18 = convertIdcarBy15bit(idcard);   
                }   
                // 获取省份   
                String provinceId = idcard18.substring(0, 2);    
                this.province = this.cityCodeMap.get(provinceId);    
                // 获取性别   
                String id17 = idcard18.substring(16, 17);   
                if (Integer.parseInt(id17) % 2 != 0) {   
                    this.gender = "M";   
                } else {   
                    this.gender = "F";   
                }   
                // 获取出生日期   
                String birthday = idcard18.substring(6, 14);   
                Date birthdate = null;
				try {
					birthdate = new SimpleDateFormat("yyyyMMdd").parse(birthday);
				} catch (ParseException e) {
					LoggerUtil.logError(this.getClass().getName(), "IdcardInfoExtractor",e);
				}   
                this.birthday = birthday;   
                Calendar currentDay = Calendar.getInstance();   
                currentDay.setTime(birthdate);   
                this.year = currentDay.get(Calendar.YEAR);   
                this.month = currentDay.get(Calendar.MONTH) + 1;   
                this.day = currentDay.get(Calendar.DAY_OF_MONTH);   
        } 
    }   
 

    /**  
     * @return the province  
     */  
    public String getProvince() {   
        return province;   
    }   

    /**  
     * @return the city  
     */  
    public String getCity() {   
        return city;   
    }   
 

    /**  
     * @return the region  
     */  
    public String getRegion() {   
        return region;   
    }   
  

    /**  
     * @return the year  
     */  
    public int getYear() {   
        return year;   
    }   


    /**  
     * @return the month  
     */  
    public int getMonth() {   
        return month;   
    }   


    /**  
     * @return the day  
     */  
    public int getDay() {   
        return day;   
    }   
  

    /**  
     * @return the gender  
     */  
    public String getGender() {   
        return gender;   
    }   


    /**  
     * @return the birthday  
     */  
    public String getBirthday() {   
        return birthday;   
    }   



    @Override  
    public String toString() {   
        return "省份：" + this.province + ",性别：" + this.gender + ",出生日期："+ this.year + "年" + this.month + "月" + this.day + "日";   
    }   

    public static void main(String[] args) throws Exception {   

        String idcard = "513822198610167011";   

        IdcardInfoExtractor ie = new IdcardInfoExtractor(idcard);   

        System.out.println(ie.toString());   

    }   
}  
